package com.function;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.vo.System_interface;

public class Sms {


	/**
	 * 逐条发送短信 sms样例代码
	 * @param httpUrl
	 * @param httpArg
	 * @return
	 */
	public static String request(String mobile, String content) {
		BufferedReader reader = null;
		String result = null;
		StringBuffer sbf = new StringBuffer();

		// 获取短信url、用户名、密码
		System_interface systeminterface = System_interface.dao.findById(1);
		String httpUrl = systeminterface.getStr("system_interface_smsurl") + "?";

		StringBuffer httpArg = new StringBuffer();
		String account = systeminterface.getStr("system_interface_smsaccount");
		httpArg.append("u=").append(account).append("&");
		String password = systeminterface
				.getStr("system_interface_smspassword");
		httpArg.append("p=").append(md5(password)).append("&");
		
		httpArg.append("m=").append(mobile).append("&");
		httpArg.append("c=").append(encodeUrlString(content, "UTF-8"));

		httpUrl = httpUrl + "?" + httpArg;
		try {
			URL url = new URL(httpUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			connection.connect();
			InputStream is = connection.getInputStream();
			reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String strRead = reader.readLine();
			if (strRead != null) {
				sbf.append(strRead);
				while ((strRead = reader.readLine()) != null) {
					sbf.append("\n");
					sbf.append(strRead);
				}
			}
			reader.close();
			result = sbf.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 发送短信
	 * @param sendInfo key手机号 value短信内容
	 * @return list中最后一个元素标识是否全部发送成功 0-全部发送成功 1-有发送失败的情况，前面每个元素分别表示每条信息的发送结果
	 */
	public static List<String> request(Map<String, String> sendInfo) {
		BufferedReader reader = null;
		List<String> rstList = new ArrayList<String>();
		String result = null;
		StringBuffer sbf = new StringBuffer();
		// 获取短信url、用户名、密码
		System_interface systeminterface = System_interface.dao.findById(1);
		String httpUrl = systeminterface.getStr("system_interface_smsurl") + "?";

		StringBuffer httpArg = new StringBuffer();
		String account = systeminterface.getStr("system_interface_smsaccount");
		httpArg.append("u=").append(account).append("&");
		String password = systeminterface
				.getStr("system_interface_smspassword");
		httpArg.append("p=").append(md5(password)).append("&");
		
		// 循环发送短信
		Set<String> mobileSet = sendInfo.keySet();
		boolean flag = true;
		for(String mobile : mobileSet){
			httpArg.append("m=").append(mobile).append("&");
			httpArg.append("c=").append(encodeUrlString(sendInfo.get(mobile), "UTF-8"));

			httpUrl = httpUrl + "?" + httpArg;

			try {
				URL url = new URL(httpUrl);
				HttpURLConnection connection = (HttpURLConnection) url.openConnection();
				connection.setRequestMethod("GET");
				connection.connect();
				InputStream is = connection.getInputStream();
				reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
				String strRead = reader.readLine();
				if (strRead != null) {
					sbf.append(strRead);
					while ((strRead = reader.readLine()) != null) {
						sbf.append("\n");
						sbf.append(strRead);
					}
				}
				reader.close();
				result = sbf.toString();
				if(!"0".equals(result)){
					flag = false;
				}
				rstList.add(result);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(flag == true){
			rstList.add("0");
		}else{
			rstList.add("1");
		}
		return rstList;
	}
	
	public static String md5(String plainText) {
		StringBuffer buf = null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();
			int i;
			buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return buf.toString();
	}

	public static String encodeUrlString(String str, String charset) {
		String strret = null;
		if (str == null)
			return str;
		try {
			strret = java.net.URLEncoder.encode(str, charset);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return strret;
	}
	
/*	public static void main(String[] args) {
		

	}
*/	
	/**
	 * 逐条发送短信 sms样例代码
	 * @param httpUrl
	 * @param httpArg
	 * @return
	 */
/*	public static String request(String httpUrl, String httpArg) {
		BufferedReader reader = null;
		String result = null;
		StringBuffer sbf = new StringBuffer();
		httpUrl = httpUrl + "?" + httpArg;

		try {
			URL url = new URL(httpUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			connection.connect();
			InputStream is = connection.getInputStream();
			reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String strRead = reader.readLine();
			if (strRead != null) {
				sbf.append(strRead);
				while ((strRead = reader.readLine()) != null) {
					sbf.append("\n");
					sbf.append(strRead);
				}
			}
			reader.close();
			result = sbf.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}*/


}
